package com.fanbuchi.fukua.manager;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.fanbuchi.fukua.application.SuperApp;
import com.fanbuchi.fukua.entity.OauthEntity;
import com.fanbuchi.fukua.entity.UserEntity;
import com.fasterxml.jackson.core.JsonProcessingException;

import java.io.IOException;

/**
 * Created by Administrator on 2015/7/10.
 */
public class UserManager {
    private static UserEntity userEntity;

    public final static UserEntity getUserEntity() {
        if (userEntity==null){
            SharedPreferences sharedPreferences= SuperApp.getApplication().getSharedPreferences("profile", Context.MODE_PRIVATE);
            String profile=sharedPreferences.getString("profile", null);
            if (TextUtils.isEmpty(profile)||profile.length()<=0){
                return null;
            }
            try {
                userEntity=SuperApp.getMapper().readValue(profile,UserEntity.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return userEntity;
    }

    public final  static void setUserEntity(UserEntity userEntity) {
        UserManager.userEntity=userEntity;
        String profile=null;
        try {
            profile=SuperApp.getMapper().writeValueAsString(UserManager.userEntity);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        SharedPreferences sharedPreferences=SuperApp.getApplication().getSharedPreferences("profile", Context.MODE_PRIVATE);
        sharedPreferences.edit().putString("profile",profile).commit();
    }


}
